package com.frejoys.app.controller;

import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.app.AddUserAddressDto;
import com.frejoys.common.dto.app.UpdateAddressDto;
import com.frejoys.common.dto.app.UserAddressIdDto;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.app.UserAddressVo;
import com.frejoys.service.UserAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "用户收货地址", description = "用户收货地址")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixApp)
 @Authorization(Authority.Private)
public class UserAddressController {

    private final UserAddressService userAddressService;


    @Operation(summary = "添加收货地址")
    @PostMapping("/user/addUserAddress")
    public Result addUserAddress(@RequestBody @Validated AddUserAddressDto addUserAddressDto) {
        userAddressService.addUserAddress(RequestUtil.userId(), addUserAddressDto);
        return Result.success();
    }

    @Operation(summary = "查看我的收货地址")
    @PostMapping("/user/userAddressList")
    public Result<List<UserAddressVo>> userAddressList() {
        return Result.success(userAddressService.userAddressList(RequestUtil.userId()));
    }


    @Operation(summary = "设置默认收获地址")
    @PostMapping("/user/setOnAddress")
    public Result setOnAddress(@RequestBody @Validated UserAddressIdDto dto) {
        userAddressService.setOnAddress(RequestUtil.userId(),dto.getId());
        return Result.success();
    }


    @Operation(summary = "删除收获地址")
    @PostMapping("/user/deleteAddress")
    public Result deleteAddress(@RequestBody @Validated UserAddressIdDto dto) {
        userAddressService.deleteAddress(dto.getId());
        return Result.success();
    }


    @Operation(summary = "修改收获地址信息")
    @PostMapping("/user/updateAddress")
    public Result updateAddress(@RequestBody @Validated UpdateAddressDto dto) {
        userAddressService.updateAddress(dto);
        return Result.success();
    }

}
